package com.dhj.jvmstudy.chapter05;

/**
 * 标量替换测试
 * -Xmx100m -Xms100m -XX:+DoEscapeAnalysis -XX:+PrintGC -XX:-EliminateAllocations
 */
public class ScalarReplace {

    public static class User {
        public int id;
        public String name;
    }

    public static void alloc() {
        User user = new User();  // 未发生逃逸
        user.id = 5;
        user.name = "dahuajia";
    }

    public static void main(String[] args) {
        long start = System.currentTimeMillis();
        for (int i = 0; i < 10000000; i++) {
            alloc();
        }
        long end = System.currentTimeMillis();

        /**
         * -XX:-EliminateAllocations 关闭标量替换，65ms
         * -XX:+EliminateAllocations 开启标量替换，8ms（未发生GC）
         */
        System.out.println("耗时：" + (end - start) + " ms");
    }

}
